﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-13>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：设置代灌户
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using System.Text;

namespace Web.Pages.QuShui.DiXiaS._2.YongHuGL
{
    public partial class SheZhiDGH : BasePage
    {
        #region 属性

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量
        /// <summary>
        /// 取水用途
        /// </summary>
        QS_YH_DAIGUANH _QS_YH_DAIGUANH = new QS_YH_DAIGUANH();

        /// <summary>
        /// 取水用途bll
        /// </summary>
        QS_YH_DAIGUANHBLL _QS_YH_DAIGUANHBLL = new QS_YH_DAIGUANHBLL();

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        QS_YG_YONGHUXXBLL _QS_YG_YONGHUXXBLL = new QS_YG_YONGHUXXBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        SpHelper _sphelper = new SpHelper();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
                LoadData();
            }
            //每次回发时从内存中取得数据源重新绑定,否则列头无法排序
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            if (e.Parameter == "info")
            {
                DataTable dtxx = _QS_YH_DAIGUANHBLL.SelectQS_YH_DAIGUANHsDynamic(string.Format("i_jlzt=0 and id={0}", id), null);
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                cr.AddValue(dtxx.Rows[0]["S_QUSHUIBH"], dtxx.Rows[0]["S_QUSHUIMC"], dtxx.Rows[0]["S_MIAOSHU"]);
                e.Result = cr.ToString();
                return;
            }
            else if (e.Parameter == "del")
            {
                this._QS_YH_DAIGUANH = _QS_YH_DAIGUANHBLL.SelectQS_YH_DAIGUANH(id);

                //设置逻辑删除
                this._QS_YH_DAIGUANH.I_JLZT = -1;
                //执行操作
                _SYS_LOGBLL.Log<QS_YH_DAIGUANH>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "设置代灌户", OperateState.Delete, _QS_YH_DAIGUANH, null);
                if (_QS_YH_DAIGUANHBLL.InsertOrUpdate(_QS_YH_DAIGUANH, null))
                {
                    
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "操作成功！";
                    e.Result = cr.ToString();
                    return;

                }
            }
            else if (e.Parameter == "save")
            {
                OperateState operateState;
                operateState = OperateState.Add;
                //添加
                if (id == -1)
                {
                    this._QS_YH_DAIGUANH.ID = 0;
                    this._QS_YH_DAIGUANH.I_JLZT = 0;
                    this._QS_YH_DAIGUANH.I_TYPE = 0;
                    this._QS_YH_DAIGUANH.I_YONGHULX = 1;
                    
                }
                int y = Convert.ToInt32(this.cmbNianF2.Text);
                DataTable dt_yz = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format(" i_jlzt=0 and s_yonghuh='{0}'", this.txtBianHao.Text), null);
                if (dt_yz != null && dt_yz.Rows.Count > 0)
                {
                    DataTable dt_yz2 = _QS_YH_DAIGUANHBLL.SelectQS_YH_DAIGUANHsDynamic(string.Format("i_jlzt=0 and i_type=0 and s_huhao='{0}' and i_y={1}", this.txtBianHao.Text,y), null);
                    if (dt_yz2.Rows.Count > 0)
                    {
                        cr.IsSuccess = false;
                        cr.IsAlertMsg = true;
                        cr.Msg = "输入的用户号已经是待灌户，请重新输入！";
                        e.Result = cr.ToString();
                        return;
                    }
                    _QS_YH_DAIGUANH.S_HUHAO = this.txtBianHao.Text;
                    _QS_YH_DAIGUANH.I_Y = y;
                    //设置操作信息
                    _QS_YH_DAIGUANH.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                    _QS_YH_DAIGUANH.D_CAOZUORQ = DateTime.Now;
                    _SYS_LOGBLL.Log<QS_YH_DAIGUANH>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "设置代灌户", operateState, _QS_YH_DAIGUANH, null);
                    if (_QS_YH_DAIGUANHBLL.InsertOrUpdate(_QS_YH_DAIGUANH, null))
                    {
                        
                        cr.IsSuccess = true;
                        cr.IsAlertMsg = true;
                        cr.Msg = "保存成功！";
                        cr.SpecialIndetity = _QS_YH_DAIGUANH.ID.ToString();
                        e.Result = cr.ToString();
                        return;
                    }
                }
                else
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    cr.Msg = "输入的用户号不存在，请重新输入！";
                    // cr.SpecialIndetity = _QS_YH_DAIGUANH.ID.ToString();
                    e.Result = cr.ToString();
                    return;
                }
               
            }
            else if (e.Parameter == "yhm") 
            {
                DataTable dt = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format(" i_jlzt=0 and s_yonghuh='{0}'", this.txtBianHao.Text), null);
                if (dt.Rows.Count>0)
                {
                     cr.IsSuccess = true;
                        cr.IsAlertMsg = true;
                        cr.Msg = dt.Rows[0]["S_YONGHUM"].ToString();
                        e.Result = cr.ToString();
                        return;
                }
            }

        }

        /// <summary>
        /// 年份切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbNianF1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
            cmbNianF2.Text = cmbNianF1.Text;
        }
        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            int year=DateTime.Now.Year;
            for (int i =year-10;  i <= year; i++)
			{
                cmbNianF1.Items.Add(i.ToString(), i);
                cmbNianF2.Items.Add(i.ToString(), i);
			}
            cmbNianF1.SelectedIndex = 10;
            cmbNianF2.SelectedIndex = 10;
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            int y=Convert.ToInt32(cmbNianF1.Value);
            this.dtSource = _sphelper.P_QSYH_SZDGH(0, y).Tables[0];
                //_QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("s_yonghuh in (select s_huhao from QS_YH_DAIGUANH where i_jlzt=0 and i_y={0} and i_type=0) and i_jlzt=0", cmbNianF1.Value), null);
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        #endregion        


    }
}